﻿using Management.Auxiliary;
using Management.Core.SteelWire;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace Management.Areas.Control.Controllers
{
    [AdminAuthorize]
    public class SteelWireController : Controller
    {
        private SteelWireManager swm = new SteelWireManager();
        //
        // GET: /Control/SteelWire/
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="SteelWireId"></param>
        /// <param name="name"></param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public ActionResult PageList(int? SteelWireId, string name, int? pageNumber, int? pageSize, int? order)
        {
            Paging<SteelWire> _pagingSteelWire = new Paging<SteelWire>();
            if (pageNumber != null && pageNumber > 0) _pagingSteelWire.PageIndex = (int)pageNumber + 1;
            if (pageSize != null && pageSize > 0) _pagingSteelWire.PageSize = (int)pageSize;
            var _paging = swm.FindPageList(_pagingSteelWire, SteelWireId, name, null);
            return Json(new { total = _paging.TotalNumber, rows = _paging.Items });
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            List<SelectListItem> _listItems = new List<SelectListItem>(2);
            _listItems.Add(new SelectListItem() { Text = "开启", Value = "1" });
            _listItems.Add(new SelectListItem() { Text = "禁止", Value = "0" });
            ViewBag.IsDisabled = _listItems;
            return PartialView();
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="_steelWire"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]

        public ActionResult Add([Bind(Include = "SteelWire, Name, IsDisabled, Created_at, Updated_at")]SteelWire _steelWire)
        {
            _steelWire.Created_at = DateTime.Now;
            _steelWire.Updated_at = DateTime.Now;
            Response _resp = new Response() { Code = -1 };
            if (ModelState.IsValid)
            {
                //检查名称
                if (swm.IsContains(u => u.Name == _steelWire.Name))
                {
                    _resp.Code = 0;
                    _resp.Message = "类型已存在";
                }
                else
                {
                    _resp = swm.Add(_steelWire);
                }
            }
            else
            {
                _resp.Code = 0;
                _resp.Message = General.GetModelErrorString(ModelState);
            }
            return Json(_resp);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="steelWireid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(int steelWireid)
        {
            Response _resp = new Response();
            var _steelWireid = swm.Find(steelWireid);
            if (_steelWireid == null)
            {
                _resp.Code = 0;
                _resp.Message = "要删除条目不存在";
            }
            else
            {
                _resp = swm.Delete(_steelWireid);
            }
            return Json(_resp);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Modify(int id)
        {
            List<SelectListItem> _listItems = new List<SelectListItem>(2);
            _listItems.Add(new SelectListItem() { Text = "开启", Value = "1" });
            _listItems.Add(new SelectListItem() { Text = "禁止", Value = "0" });
            ViewBag.IsDisabled = _listItems;
            return PartialView(swm.Find(id));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Modify(int id, FormCollection form)
        {
            Response _resp = new Response();
            var _sweelwire = swm.Find(id);
            _sweelwire.Updated_at = DateTime.Now;
            if (TryUpdateModel(_sweelwire, new string[] { "Name", "IsDisabled", "Updated_at" }))
            {
                if (swm.IsContains(u => u.Name == _sweelwire.Name) && form["Name"] != _sweelwire.Name)
                {
                    _resp.Code = 0;
                    _resp.Message = "名称已存在";
                }
                else
                {
                    _resp = swm.Update(_sweelwire);
                }
            }
            else
            {
                _resp.Code = 0;
                _resp.Message = "模型不符合条件，更新失败";
            }
            return Json(_resp);
        }
	}
}